function z = respfct(x)
% function z = respfct(x)
%    this function evaluates the nonlinear residual
%    for the current balance and keff estimate

% declare the global problem variables
global ne dc sa ns sw bcL bcR

% normalize currents to unity
x(1:end-1,1)=x(1:end-1,1)/sum(x(1:end-1,1));

% compute the response matrices
M = connect(ne,bcL,bcR);
[R F A L M] = respmtx(ne, dc, sa, ns, sw, x);

% compute the nonlinear function, FF(x)*x = 0
FF = [ R*M-eye(ne*2)  zeros(ne*2,1) 
       F*M            -sum(A*M*x(1:end-1))-sum(L)    ];  
z = FF*x;